 package cn.lfy.studentmanager.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.lfy.studentmanager.domain.Academy;
import cn.lfy.studentmanager.domain.AjaxGrid;
import cn.lfy.studentmanager.domain.AjaxRst;
import cn.lfy.studentmanager.domain.Student;
import cn.lfy.studentmanager.service.AcademyService;


@Controller
@RequestMapping("/academy")
public class AcademyController {
	
	@Autowired
	private AcademyService academyService;
	//为了实现某些方法，而引入service
	
	@GetMapping("index")
	public String index(ModelMap modelMap) {
		return "baseinfo/academy";
	}
	//访问到主页面
	
	@PostMapping("add")
	@ResponseBody
	public Object add(Academy academy, ModelMap modelMap){
		
		String msg = "";
		boolean isSuccess = academyService.add(academy);
		if(isSuccess) {
			msg = "新增成功";
		}else {
			msg = "新增失败";
		}
		return new AjaxRst(isSuccess, msg);
	}
	//新增学院，操作结果默认为空，定义Boolean型变量（只有true和false结果），如果新增成功，如果失败。。
	
	@GetMapping("select")
	@ResponseBody
	public Object select(@RequestParam( required = true)int page,@RequestParam( required = true)int limit) {
		List<Academy> list = academyService.select(page,limit);
		//容器：里面装的数据类型
		AjaxGrid<Academy> ajaxGrid = new AjaxGrid<Academy>();
		
		ajaxGrid.setCode(0);
		ajaxGrid.setCount(academyService.getCount());
		ajaxGrid.setMsg("查询成功");
		ajaxGrid.setData(list);
		return ajaxGrid;
	}
	//将新增后的所有数据从数据库中查询并显示在页面,但是为了将数据输出，必须按照jQuery法则来，
	

	//删除信息
	@GetMapping("delete")
	@ResponseBody
	public Object delete(@RequestParam( required = true)int id , ModelMap modelMap) {
	//给出要删除的参数  @RequestParam( required = true)int id 
	//System.out.println(id);
	boolean isSuccess = academyService.delete(id);
	String msg = "删除失败!";
	if(isSuccess) {
		msg = "删除成功";
	}
	return new AjaxRst(isSuccess, msg);
	}
	
	//编辑完成保存时
	@PostMapping("edit")
	@ResponseBody
	public Object updatesuccess(@RequestParam( required = true)int id,@RequestParam( required = true)String name,ModelMap modelMap){
		//调用service中的
		String msg = "";
		boolean isSuccess = academyService.update(id,name);
		if(isSuccess) {
			msg = "新增成功";
		}else {
			msg = "新增失败";
		}
		return new AjaxRst(isSuccess, msg);
	}
	
}